package com.atguigu.service.impl;

import com.atguigu.pojo.vo.PortalVo;
import com.atguigu.utils.JwtHelper;
import com.atguigu.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.pojo.Headline;
import com.atguigu.service.HeadlineService;
import com.atguigu.mapper.HeadlineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
/**
* @author ASUS
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2025-08-16 14:36:55
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{
    @Autowired
    private HeadlineMapper headlineMapper;
    @Autowired
    private JwtHelper jwtHelper;
    /**
     * 进行分页数据查询
     * 分页数据，拼接到result即可
     * 注意1：查询定义的语句，自定义mapper方法，携带分页
     * 注意2：返回结果用一个map装，多个map构成了一个集合
     * @param portalVo
     * @return
     */
    @Override
    public Result findNewsPage(PortalVo portalVo) {
        IPage<Headline> page = new Page<>(portalVo.getPageNum(),portalVo.getPageSize());
        headlineMapper.selectPageMap(page,portalVo);
        List<Headline> records = page.getRecords();
        Map data = new HashMap();
        data.put("pageData",records);
        data.put("pageNum",page.getCurrent());
        data.put("pageSize",page.getSize());
        data.put("totalPage",page.getTotal());
        data.put("totalSize",page.getTotal());

        HashMap map = new HashMap<>();
        map.put("pageInfo",data);

        return Result.ok(map);
    }

    /**
     * 1,修改阅读量version需要知道版本号
     * 2.查询对应的数据---多表查询
     * @param hid
     * @return
     */
    @Override
    public Result showHeadlineDetail(Integer hid) {
        Map map = headlineMapper.queryDeatailMap(hid);
        Map headlineMap = new HashMap();
        headlineMap.put("headline",map);
        //修改阅读量加一
        Headline headline = new Headline();
        headline.setHid((Integer) map.get("hid"));
        headline.setVersion((Integer) map.get("version"));
        //阅读量+1
        headline.setPageViews((Integer) map.get("pageViews") + 1);
        headlineMapper.updateById(headline);

        return Result.ok(headlineMap);
    }

    /**
     * 数据库的数据插入
     * @param headline
     * @return
     */
    @Override
    public Result publish(Headline headline,String token) {
        //根据token找用户ID
        int userId = jwtHelper.getUserId(token).intValue();
        //数据装配
        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        int i = headlineMapper.insert(headline);
        return Result.ok(null);
    }
    /**
     * 1.根据Hid查询数据最新的version
     * 2.修改数据的修改时间为当前时间
     */
    @Override
    public Result updateData(Headline headline) {
        int version = headlineMapper.selectById(headline.getHid()).getVersion().intValue();//拿到version的最新版本
        headline.setVersion(version);//乐观锁
        headline.setUpdateTime(new Date());
        headlineMapper.updateById(headline);
        return Result.ok(null);
    }
}




